package cn.kgc.tangcco.dao.impl;

import cn.kgc.tangcco.dao.AccountMongoDao;
import cn.kgc.tangcco.pojo.Account;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 李昊哲
 * @Description
 * @create 2020/12/29 9:57
 */
@Component
public class AccountMongoDaoImpl  implements AccountMongoDao {
    @Resource
    MongoTemplate mongoTemplate;

    @Override
    public Account findAccount(String mobile) {
        Query query = new Query();
        Criteria criteria = Criteria.where("mobile").is(mobile);
        query.addCriteria(criteria);
        List<Account> accounts = mongoTemplate.find(query, Account.class);
        if (accounts == null || accounts.size() == 0) {
            return null;
        }else {
            return accounts.get(0);
        }
    }

    @Override
    public List<Account> findAll() {
        return mongoTemplate.findAll(Account.class);
    }

    @Override
    public Account save(Account account) {
        return mongoTemplate.save(account);
    }

    @Override
    public void saveAll(List<Account> accounts) {
        mongoTemplate.insertAll(accounts);
    }
}
